<template>
  <!-- 资产点检工单表单弹窗 -->
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      width="900px"
      draggable
      @close="closeDialog"
    >
      <h4 class="mb-[20px] mt-[0px]">基本数据</h4>
      <el-form
        v-show="!props.isReadonly"
        ref="formRef"
        :model="formData"
        :inline="true"
        :rules="rules"
        label-width="160"
      >
        <div>
          <el-row>
            <el-col :span="6">
              <el-form-item label="资产大类" prop="assetKind">
                <el-input
                  v-model="formData.assetKind"
                  placeholder="请输入资产大类"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="工单编号" prop="orderNo">
                <el-input
                  v-model="formData.orderNo"
                  placeholder="请输入工单编号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="作业时间" prop="workTime">
                <el-input
                  v-model="formData.workTime"
                  placeholder="请输入作业时间"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="作业类型主键" prop="sopTypeId">
                <el-input
                  v-model="formData.sopTypeId"
                  placeholder="请输入作业类型主键"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="作业类型编号" prop="sopTypeCode">
                <el-input
                  v-model="formData.sopTypeCode"
                  placeholder="请输入作业类型编号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标定标准作业主键" prop="sopStandardId">
                <el-input
                  v-model="formData.sopStandardId"
                  placeholder="请输入标定标准作业主键"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="标定标准作业编号" prop="sopStandardCode">
                <el-input
                  v-model="formData.sopStandardCode"
                  placeholder="请输入标定标准作业编号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="资产主键" prop="assetId">
                <el-input
                  v-model="formData.assetId"
                  placeholder="请输入资产主键"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="资产编号" prop="assetCode">
                <el-input
                  v-model="formData.assetCode"
                  placeholder="请输入资产编号"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="资产名称" prop="assetName">
                <el-input
                  v-model="formData.assetName"
                  placeholder="请输入资产名称"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="工单状态" prop="orderStatus">
                <el-input
                  v-model="formData.orderStatus"
                  placeholder="请输入工单状态"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="备注" prop="remarks">
                <el-input
                  v-model="formData.remarks"
                  placeholder="请输入备注"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="执行人主键" prop="executePersonId">
                <el-input
                  v-model="formData.executePersonId"
                  placeholder="请输入执行人主键"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="执行人" prop="executePersonName">
                <el-input
                  v-model="formData.executePersonName"
                  placeholder="请输入执行人"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="复核人主键" prop="checkPersonId">
                <el-input
                  v-model="formData.checkPersonId"
                  placeholder="请输入复核人主键"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="复核人" prop="checkPersonName">
                <el-input
                  v-model="formData.checkPersonName"
                  placeholder="请输入复核人"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="是否删除" prop="deleted">
                <el-input
                  v-model="formData.deleted"
                  placeholder="请输入是否删除"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建时间" prop="createTime">
                <el-input
                  v-model="formData.createTime"
                  placeholder="请输入创建时间"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建人" prop="createdUserId">
                <el-input
                  v-model="formData.createdUserId"
                  placeholder="请输入创建人"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="创建人名" prop="createdUserName">
                <el-input
                  v-model="formData.createdUserName"
                  placeholder="请输入创建人名"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="更新时间" prop="lastUpdateTime">
                <el-input
                  v-model="formData.lastUpdateTime"
                  placeholder="请输入更新时间"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="更新人" prop="lastUpdatedUserId">
                <el-input
                  v-model="formData.lastUpdatedUserId"
                  placeholder="请输入更新人"
                  clearable
                />
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="更新人" prop="lastUpdatedUserName">
                <el-input
                  v-model="formData.lastUpdatedUserName"
                  placeholder="请输入更新人"
                  clearable
                />
              </el-form-item>
            </el-col>
          </el-row>
        </div>
      </el-form>

      <el-descriptions
        v-show="props.isReadonly"
        class="margin-top w-full"
        title=""
        :column="2"
        size="large"
        border
      >
        <!--        <el-descriptions-item>-->
        <!--          <template #label>-->
        <!--            <div class="cell-item">资产大类</div>-->
        <!--          </template>-->
        <!--          <div style="width: 100%; word-break: break-all">-->
        <!--            {{ useDictionary("AssetKind",formData.assetKind) || "" }}-->
        <!--          </div>-->
        <!--        </el-descriptions-item>-->
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">点检计划编号</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.planNo || "" }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">点检计划名称</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.planName || "" }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">工单编号</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.orderNo || "" }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">作业时间</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.workTime || "" }}
          </div>
        </el-descriptions-item>
        <!--        <el-descriptions-item>-->
        <!--          <template #label>-->
        <!--            <div class="cell-item">作业类型</div>-->
        <!--          </template>-->
        <!--          <div style="width: 100%; word-break: break-all">-->
        <!--            {{ formData.sopTypeName || "" }}-->
        <!--          </div>-->
        <!--        </el-descriptions-item>-->
        <!--        <el-descriptions-item>-->
        <!--          <template #label>-->
        <!--            <div class="cell-item">标准作业编号</div>-->
        <!--          </template>-->
        <!--          <div style="width: 100%; word-break: break-all">-->
        <!--            {{ formData.sopStandardCode || "" }}-->
        <!--          </div>-->
        <!--        </el-descriptions-item>-->
        <!--        <el-descriptions-item>-->
        <!--          <template #label>-->
        <!--            <div class="cell-item">标准作业名称</div>-->
        <!--          </template>-->
        <!--          <div style="width: 100%; word-break: break-all">-->
        <!--            {{ formData.sopStandardName || "" }}-->
        <!--          </div>-->
        <!--        </el-descriptions-item>-->
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">资产编号</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.assetCode || "" }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">资产名称</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.assetName || "" }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">工单状态</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ useDictionary("CheckOrderStatus", formData.orderStatus) || "" }}
          </div>
        </el-descriptions-item>
        <el-descriptions-item>
          <template #label>
            <div class="cell-item">执行人</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.executePersonName || "" }}
          </div>
        </el-descriptions-item>
        <!--        <el-descriptions-item>-->
        <!--          <template #label>-->
        <!--            <div class="cell-item">复核人</div>-->
        <!--          </template>-->
        <!--          <div style="width: 100%; word-break: break-all">-->
        <!--            {{ formData.checkPersonName || "" }}-->
        <!--          </div>-->
        <!--        </el-descriptions-item>-->
        <!--        <el-descriptions-item>-->
        <!--          <template #label>-->
        <!--            <div class="cell-item">创建时间</div>-->
        <!--          </template>-->
        <!--          <div style="width: 100%; word-break: break-all">-->
        <!--            {{ formData.createTime || "" }}-->
        <!--          </div>-->
        <!--        </el-descriptions-item>-->
        <el-descriptions-item :span="2">
          <template #label>
            <div class="cell-item">备注</div>
          </template>
          <div style="width: 100%; word-break: break-all">
            {{ formData.remarks || "" }}
          </div>
        </el-descriptions-item>
      </el-descriptions>

      <h4 class="mb-[20px] mt-[20px]">详细数据</h4>
      <template #header v-if="!props.isReadonly">
        <el-button type="success" plain @click="handleAdd">
          <i-ep-plus />
          新增</el-button
        >
      </template>
      <div class="formTable">
        <el-table
          :data="formData.details"
          style="width: 100%"
          :key="Math.random"
          max-height="25vh"
          border
        >
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="100"
          />
          <el-table-column label="点检项" props="sopItemName" min-width="150">
            <template #default="{ $index, row }">
              {{ row.sopItemName }}
            </template>
          </el-table-column>
          <el-table-column label="点检值" props="sopItemValue" min-width="150">
            <template #default="{ $index, row }">
              {{ row.sopItemValue }}
            </template>
          </el-table-column>
          <el-table-column
            label="点检结果"
            props="sopItemResult"
            min-width="100"
            align="center"
          >
            <template #default="{ $index, row }">
              {{
                row.sopItemResult === "1"
                  ? "通过"
                  : row.sopItemResult === "0"
                  ? "未通过"
                  : ""
              }}
            </template>
          </el-table-column>
          <el-table-column
            label="点检标准"
            props="sopItemStandard"
            min-width="150"
          >
            <template #default="{ $index, row }">
              {{ row.sopItemStandard }}
            </template>
          </el-table-column>
          <el-table-column label="图片" props="image" min-width="150">
            <template #default="{ $index, row }">
              <span v-for="(item, index) in row.images" :key="index">
                <img
                  :src="`${upPreviewUrl}${item.url}`"
                  style="
                    width: 30px;
                    height: 30px;
                    margin-right: 10px;
                    cursor: pointer;
                  "
                  @click="
                    handlePictureCardPreview(`${upPreviewUrl}${item.url}`)
                  "
                  alt=""
                />
              </span>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            align="center"
            label="操作"
            width="100"
            v-if="!props.isReadonly"
          >
          </el-table-column>
        </el-table>
      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";
import { useDictionary } from "@/utils/useTable"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "EAMEAMPOINTCHECKORDERADDORUPDATE",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

/** 弹窗显示 **/
const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

/** formData定义表单数据 */
const formRef = ref(ElForm);
let formData = reactive({
  id: "",
  version: "",
  assetKind: "",
  planNo: "",
  planName: "",
  orderNo: "",
  workTime: "",
  sopTypeId: "",
  sopTypeName: "",
  sopTypeCode: "",
  sopStandardId: "",
  sopStandardCode: "",
  sopStandardName: "",
  assetId: "",
  assetCode: "",
  assetName: "",
  orderStatus: "",
  remarks: "",
  executePersonId: "",
  executePersonName: "",
  checkPersonId: "",
  checkPersonName: "",
  deleted: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  details: [
    {
      id: "",
      version: "",
      orderId: "",
      seq: "",
      sopItemId: "",
      sopItemName: "",
      sopItemValue: "",
      sopItemResult: "",
      sopItemStandard: "",
      createTime: "",
      createdUserId: "",
      createdUserName: "",
      lastUpdateTime: "",
      lastUpdatedUserId: "",
      lastUpdatedUserName: "",
      sopItemValueId: "",
      sopStandardId: "",
    },
  ],
});

/** rules规则 */
const rules = reactive({
  assetKind: [],
  orderNo: [],
  workTime: [],
  sopTypeId: [],
  sopTypeCode: [],
  sopStandardId: [],
  sopStandardCode: [],
  assetId: [],
  assetCode: [],
  assetName: [],
  orderStatus: [],
  remarks: [],
  executePersonId: [],
  executePersonName: [],
  checkPersonId: [],
  checkPersonName: [],
  deleted: [],
  createTime: [],
  createdUserId: [],
  createdUserName: [],
  lastUpdateTime: [],
  lastUpdatedUserId: [],
  lastUpdatedUserName: [],
  sonsopItemName: [],
  sonsopItemValue: [],
  sonsopItemResult: [],
  sonsopItemStandard: [],
});

const isValidated = ref(false);
/** 删除行 */
const handleDelete = (index: any) => {
  formData.details.splice(index, 1);
  isValidated.value && formRef.value.validate(() => {});
};

const details = {
  id: "",
  version: "",
  orderId: "",
  seq: "",
  sopItemId: "",
  sopItemName: "",
  sopItemValue: "",
  sopItemResult: "",
  sopItemStandard: "",
  createTime: "",
  createdUserId: "",
  createdUserName: "",
  lastUpdateTime: "",
  lastUpdatedUserId: "",
  lastUpdatedUserName: "",
  sopItemValueId: "",
  sopStandardId: "",
};

/** 添加行 */
const handleAdd = () => {
  let sondata = JSON.parse(JSON.stringify(details));
  formData.details.push(sondata);
};

/** 编辑时赋值 */
watch(
  () => props.data,
  (newVal: any) => {
    formData.id = "";
    if (newVal?.id) {
      Object.assign(formData, props.data);
      if (formData.id) {
        getdetails();
      }
    }
  }
);

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  // resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.details = [JSON.parse(JSON.stringify(details))];
  formData.id = "";
  formData.version = "";
  formData.assetKind = "";
  formData.orderNo = "";
  formData.planNo = "";
  formData.planName = "";
  formData.workTime = "";
  formData.sopTypeId = "";
  formData.sopTypeName = "";
  formData.sopTypeCode = "";
  formData.sopStandardId = "";
  formData.sopStandardCode = "";
  formData.sopStandardName = "";
  formData.assetId = "";
  formData.assetCode = "";
  formData.assetName = "";
  formData.orderStatus = "";
  formData.remarks = "";
  formData.executePersonId = "";
  formData.executePersonName = "";
  formData.checkPersonId = "";
  formData.checkPersonName = "";
  formData.deleted = "";
  formData.createTime = "";
  formData.createdUserId = "";
  formData.createdUserName = "";
  formData.lastUpdateTime = "";
  formData.lastUpdatedUserId = "";
  formData.lastUpdatedUserName = "";
}

const loading = ref(false);

const uploadApi = import.meta.env.VITE_APP_BASE_API;
// 预览url前缀
const upPreviewUrl = `${uploadApi}/system/fileCenter/preview/`;

const handlePictureCardPreview = (url: string) => {
  url = url?.replace("fileCenter/download/", "fileCenter/preview/");
  window.open(url);
};
/** 资产点检工单保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;
      const text = formData.id ? "编辑" : "新增";
      console.log("formData", toRaw(formData));
      await request({
        url: "/eam/eampointcheckorder/save",
        method: "post",
        data: formData,
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      closeDialog();
      emit("reset-table", formData.id);
      isValidated.value = false;
    } else {
      isValidated.value = true;
    }
  });
}

async function getdetails() {
  let sondetails = await request({
    url: "/eam/eampointcheckorder/getDetails",
    method: "post",
    data: formData,
  }).finally(() => {
    loading.value = false;
  });
  formData.details = sondetails.data;
}
</script>

<style lang="scss" scoped>
h4 {
  font-weight: 400;
  color: #000;
  padding-left: 10px;
  font-size: 14px;
  line-height: 14px;
}
.formTable {
  :deep(.el-form-item) {
    margin-right: 0 !important;
  }
}
:deep(.el-dialog .el-dialog__body) {
  overflow-y: auto;
  height: 65vh;
}
:deep(.search-container .el-input.el-input--default.el-input--suffix) {
  // 固定宽度
  width: 190px !important;
}
:deep(.search-container .el-form-item--default .el-form-item__label) {
  // 固定宽度
  justify-content: end;
}
</style>
